package io.renren.modules.school.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import io.renren.common.utils.Constant;
import io.renren.common.validator.ValidatorUtils;
import io.renren.modules.school.entity.*;
import io.renren.modules.school.service.SchoolPredictorService;
import io.renren.modules.school.service.SchoolYearService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import io.renren.modules.school.service.SchoolService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;



/**
 * 
 *
 * @author xuebing
 * @email xuebinggod@163.com
 * @date 2018-10-11 19:12:15
 */
@RestController
@RequestMapping("school/school")
public class SchoolController {
    @Autowired
    private SchoolService schoolService;
    @Autowired
    private SchoolPredictorService schoolPredictorService;
    @Autowired
    private SchoolYearService schoolYearService;

    /**
     * 列表
     */
    @RequestMapping("/list")
   // @RequiresPermissions("school:school:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = schoolService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     * 列表
     */
    @RequestMapping("/getUniversitys")
    // @RequiresPermissions("school:school:list")
    public R getUniversitys(@RequestBody Map<String, Object> params){
        List l=schoolService.getUniversitys(params);
        return R.ok().put("universitys",l);
    }

    /**
     * 列表
     */
    @RequestMapping("/getLastYearScore")
    // @RequiresPermissions("school:school:list")
    public R getLastYearScore(@RequestBody Map<String, Object> params){
        List<LastThreeYear> l=schoolService.getLastYearScore(params);
                    String lty="";
            for(LastThreeYear lastThreeYear:l){
                lty=lty+lastThreeYear.getYear()+" : "+lastThreeYear.getPredictor()+"分; ";
            }
        return R.ok().put("lastThreeYear",lty);
    }




    /**
     * 列表
     */
    @RequestMapping("/schoolPredictor")
    // @RequiresPermissions("school:school:list")
    public R schoolPredictor(@RequestParam Map<String, Object> params){
        PageUtils page = schoolPredictorService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
   // @RequiresPermissions("school:school:info")
    public R info(@PathVariable("id") Integer id){
        SchoolEntity school = schoolService.selectById(id);

        return R.ok().put("school", school);
    }

    /**
     * 信息
     */
    @RequestMapping("/schoolPredictor/{id}")
    // @RequiresPermissions("school:school:info")
    public R schoolPredictor(@PathVariable("id") Integer id){
        SchoolPredictor school = schoolPredictorService.selectById(id);

        return R.ok().put("school", school);
    }


    @RequestMapping("/schoolYear/{id}")
    // @RequiresPermissions("school:school:info")
    public R schoolYear(@PathVariable("id") String id){
        LowscoreEntity lowscore = schoolYearService.selectById(id);

        return R.ok().put("lowscore", lowscore);
    }

    @RequestMapping("/updateLowScore")
    // @RequiresPermissions("school:school:info")
    public R updateLowScore(@RequestBody LowscoreEntity  lowscoreEntity){
         schoolYearService.updateById(lowscoreEntity);

        return R.ok();
    }





    /**
     * 保存
     */
    @RequestMapping("/save")
   // @RequiresPermissions("school:school:save")
    public R save(@RequestBody SchoolEntity school){
        schoolService.insert(school);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
   // @RequiresPermissions("school:school:update")
    public R update(@RequestBody SchoolEntity school){
        ValidatorUtils.validateEntity(school);
        schoolService.updateAllColumnById(school);//全部更新
        
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/updatePredictor")
    // @RequiresPermissions("school:school:update")
    public R updatePredictor(@RequestBody SchoolPredictor school){
        //ValidatorUtils.validateEntity(school);
        schoolPredictorService.updateAllColumnById(school);//全部更新

        return R.ok();
    }


    /**
     * 删除
     */
    @RequestMapping("/delete")
  //  @RequiresPermissions("school:school:delete")
    public R delete(@RequestBody Integer[] ids){
        schoolService.deleteBatchIds(Arrays.asList(ids));

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/setLastThreeYear")
    //  @RequiresPermissions("school:school:delete")
    public R setLastThreeYear(@RequestBody String[] year){
      //  schoolService.deleteBatchIds(Arrays.asList(ids));
        schoolService.setLastThreeYear(year);
        return R.ok();
    }



    /**
     * 删除
     */
    @RequestMapping("/changeALL")
    //  @RequiresPermissions("school:school:delete")
    public R changeALL(@RequestBody  Map<String, Object> params){
        schoolService.changeALL(params);

        return R.ok();
    }


    /**
     * 获得学校列表
     */
    @RequestMapping("/getSchoolTree")
//    @RequiresPermissions("course:school:select")
    public R getSchoolTree(){
        List<TreeEntity> schoolList = schoolService.getSchoolTree();

        return R.ok().put("schoolList", schoolList);
    }


    /**
     * 获得学校最低分
     */
    @RequestMapping("/getSchoolLowestScore")
//    @RequiresPermissions("course:school:select")
    public R getListBySchoolId(Integer id){
       // List<LowscoreEntity> lowScoreList = schoolService.getListBySchoolId(id);
        PageUtils page = schoolService.getListBySchoolId(id);
        return R.ok().put("page", page);
    }

    /**
     * 保存学校最低分
     */
    @RequestMapping("/saveLowScore")
    // @RequiresPermissions("school:school:save")
    public R saveLowScore(@RequestBody LowscoreEntity school){
       // schoolService.insert(school);
        SchoolEntity schoolEntity=schoolService.selectById(school.getSchoolId());
        school.setSchoolName(schoolEntity.getName());
        school.setSchoolCode(schoolEntity.getCode());
        LowscoreEntity l=schoolService.getLowscoreEntityByYear(school);
        if(l==null) {
            schoolService.insertLowScore(school);
            return R.ok();
        }else{
            return R.ok("此年份已存在",101);
        }
    }


    /**
     * 保存学校最低分
     */
    @RequestMapping("/deleteLowScore")
    // @RequiresPermissions("school:school:save")
    public R deleteLowScore( Integer id){
        // schoolService.insert(school);
            schoolService.deleteLowScore(id);
            return R.ok();
    }

}
